package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.springboot.controller.request.QuestionPageRequest;
import com.example.springboot.entity.Question;
import com.example.springboot.mapper.QuestionMapper;
import com.example.springboot.service.IQuestionService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class QuestionService implements IQuestionService {
    @Autowired
    QuestionMapper questionMapper;
    @Override
    public List<Question> list() {
        return questionMapper.list();
    }

    @Override
    public PageInfo<Question> page(QuestionPageRequest baseRequest) {

        PageHelper.startPage(baseRequest.getPageNum(),baseRequest.getPageSize());

        List<Question> questions= questionMapper.listByCondition(baseRequest);

        return new PageInfo<>(questions);
    }

    @Override
    public void save(Question question) {
       Date date= new Date();
        //当作卡号处理
        question.setUsername(DateUtil.format(date,"yyyyMMdd")+ Math.abs(IdUtil.fastSimpleUUID().hashCode()));
        questionMapper.save(question);
    }
    @Override
    public Question getById(Integer id){

        return questionMapper.getById(id);
    }
    @Override
    public void update(Question question){
question.setUpdatetime(new Date());
        questionMapper.updateById(question);
    }
    public void  deleteById(Integer id){
        questionMapper.deleteById(id);
    }
}
